<template>
  <div class="editor-business-page">
    <div class="search p20">
      <el-button class="btn-add" type="primary" size="small" @click="handleAdd" v-hasPermi="['editor:standardRuleInfo:add']">新增</el-button>
      <div class="btns">
        <el-input
          clearable
          v-model="keyword"
          placeholder="请输入名称关键字"
          style="width: 300px"
          maxlength="100"
          size="small"
          @keyup.enter.native="getList"
        />
        <el-button class="search-btn" type="primary" size="small" @click="getList">搜索</el-button>
        <el-button type="primary" plain size="small" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="content" v-show="!ruleManageShow">
      <el-table :data="dataList" :loading="loading">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="规范名称" prop="name" />
        <el-table-column label="发布/禁用" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope" v-if="!!scope">
            <el-switch
              v-model="scope.row.isPublished"
              size="mini"
               v-hasPermi="['editor:standardRuleInfo:change']"
              @change="switchStatus(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['editor:standardRuleInfo:edit']">基础信息</el-button>
            <el-button
              v-if="scope.row.isPublished"
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleRule(scope.row, true)"
            >
              查看规范
            </el-button>
            <el-button v-else size="mini" type="text" icon="el-icon-edit" @click="handleRule(scope.row, false)">
              编辑规范
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <RulesEditModal
      :visible.sync="addShow"
      :editData="editData"
      @onClose="onCloseDialog"
      @onSuccess="onSuccess"
      :ruleList="ruleList"
    />
    <RulesManage
      v-show="ruleManageShow"
      :currentRule="currentRule"
      @onClose="ruleManageShow = false"
      :readonly="ruleManageReadonly"
    />
  </div>
</template>
<script>
import { getStandardRulesList, changeRuleStatus, getRulesList } from '@/api/business'
import RulesEditModal from '@/views/editor/__components__/RulesEditModal'
import RulesManage from '@/views/editor/__components__/RulesManage'
import { Message } from 'element-ui'

export default {
  components: { RulesEditModal, RulesManage },
  data() {
    return {
      dataList: [],
      editModalShow: false,
      editData: null,
      keyword: '',
      loading: false,
      addShow: false,
      ruleManageShow: false,
      ruleManageReadonly: false,
      currentRule: null,
      ruleList: []
    }
  },
  mounted() {
    this.ruleManageShow = false
    this.getList()
  },
  methods: {
    resetQuery() {
      this.keyword = ''
      this.$nextTick(() => {
        this.getList()
      })
    },
    getList() {
      this.loading = true
      getStandardRulesList({
        keyword: this.keyword
      })
        .then((res) => {
          this.dataList = res.data.map((i) => ({ ...i, isPublished: i.status === '1' }))
        })
        .finally(() => {
          this.loading = false
        })
    },
    onCloseDialog() {
      this.editData = {}
    },
    onSuccess() {
      this.editData = {}
      this.getList()
    },
    handleAdd() {
      this.editData = {}
      this.getRulesForModal()
    },
    handleEdit(row) {
      this.editData = row
      this.getRulesForModal()
    },
    getRulesForModal() {
      getRulesList()
        .then((res) => {
          this.ruleList = res.data
          this.addShow = true
        })
        .catch(() => {
          Message.error('规范查询失败')
        })
    },
    switchStatus(row) {
      changeRuleStatus({
        id: row.id,
        status: row.isPublished ? 1 : 0
      })
        .then((res) => {
          console.log('publishTemplate', res)
        })
        .finally(() => {
          this.getList()
        })
    },
    handleRule(row, readonly) {
      this.currentRule = row
      this.ruleManageShow = true
      this.ruleManageReadonly = readonly
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  padding: 15px 24px 30px;
}
.editor-business-page {
  display: block;
}
.editor-business-page .search {
  display: flex;
  justify-content: space-between;
}
</style>
